﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Public Class BitacoraDAL
    Public Function registraBitacora(ByVal pBitacora As BitacoraBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 8) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""Bitacora""(""Fecha"", ""Hora"", ""Id"", ""IdEmpresa"", ""Proceso"", ""Registro"", ""SubProceso"", ""Tabla"", ""Usuario"" " & _
                                        "VALUES (?,?,?,?,?,?,?,?,?);"

            oInsParm(0) = New OdbcParameter("", pBitacora.Fecha)
            oInsParm(1) = New OdbcParameter("", pBitacora.Hora)
            oInsParm(2) = New OdbcParameter("", pBitacora.ID)
            oInsParm(3) = New OdbcParameter("", pBitacora.IdEmpresa)
            oInsParm(4) = New OdbcParameter("", pBitacora.Proceso)
            oInsParm(5) = New OdbcParameter("", pBitacora.Registro)
            oInsParm(6) = New OdbcParameter("", pBitacora.SubProceso)
            oInsParm(7) = New OdbcParameter("", pBitacora.Tabla)
            oInsParm(8) = New OdbcParameter("", pBitacora.Usuario)

            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using

            
        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function getBitacoraById(ByVal pId As Integer) As BitacoraBE
        Dim instrSql As String
        Dim tmpbitacora As BitacoraBE
        Try
            instrSql = "SELECT ""Fecha"", ""Hora"", ""Id"", ""IdEmpresa"", ""Proceso"", ""Registro"", ""SubProceso"", ""Tabla"", ""Usuario"" " & _
                                     "FROM ""Bitacora"" " & _
                                    "WHERE ""IdEmpresa"" = ? ;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pId))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpbitacora = Nothing 'BitacoraFact.Build(myDR)
                        Else
                            tmpbitacora = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpbitacora
            
        Catch ex As Exception
            Throw 
        Finally
            tmpbitacora = Nothing
            instrSql = ""
        End Try
    End Function
End Class
